package myself.a009BinaryTree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author 叶之越
 * Description
 * Date 2021/1/26
 * Time 22:16
 * Mail 739153436@qq.com
 */
public class SerializeAndReconstructTree {

    // ================ 前序遍历序列化和反序列化 ================
    public static Queue<String> preSerial(Node head) {
        if (head == null) {
            return null;
        }
        Queue<String> queue = new LinkedList<>();
        pres(head, queue);
        return queue;
    }

    public static void pres(Node head, Queue<String> ans) {
        if (head == null) {
            ans.add("#");
        } else {
            ans.add(String.valueOf(head.value));
            pres(head.left, ans);
            pres(head.right, ans);
        }
    }

    public static Node buildByPreQueue(Queue<String> prelist) {
        if (prelist == null || prelist.size() == 0) {
            return null;
        }
        return preb(prelist);
    }

    public static Node preb(Queue<String> prelist) {
        if (prelist.peek() == null) {
            return null;
        }
        Node node = new Node(Integer.valueOf(prelist.poll()));
        node.left = preb(prelist);
        node.right = preb(prelist);
        return node;
    }

    // ================ 中序遍历序列化和反序列化 ================
    public static Queue<String> levelSerial(Node head) {
        Queue<String> ans = new LinkedList<>();
        if (head == null) {
            ans.add(null);
        } else {
            ans.add(String.valueOf(head.value));
            Queue<Node> queue = new LinkedList<Node>();
            queue.add(head);
            while (!queue.isEmpty()) {
                head = queue.poll(); // head 父   子
                if (head.left != null) {
                    ans.add(String.valueOf(head.left.value));
                    queue.add(head.left);
                } else {
                    ans.add(null);
                }
                if (head.right != null) {
                    ans.add(String.valueOf(head.right.value));
                    queue.add(head.right);
                } else {
                    ans.add(null);
                }
            }
        }
        return ans;
    }

    public static Node buildByLevelQueue(Queue<String> levelList) {
        if (levelList == null
                || levelList.size() == 0
                // 不要遗漏第一个元素是空的情况
                || levelList.peek() == null) {
            return null;
        }
        Queue<Node> queue = new LinkedList<>();
        Node cur = generateNode(levelList.poll());
        Node head = cur;
        queue.add(cur);

        while (!queue.isEmpty()) {
            cur = queue.poll();
            cur.left = generateNode(levelList.poll());
            cur.right = generateNode(levelList.poll());
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
        }

        return head;
    }

    public static Node generateNode(String val) {
        if (val == null) {
            return null;
        }
        return new Node(Integer.valueOf(val));
    }

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }


}
